using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;
using System.Data;

using PlugNT.Safe;
using PlugNT.Database;
using PlugNT.Database.Common.Simple;
using PlugNT.Cms.Custom;
using PlugNT.App.Payment.Model;


namespace PlugNT.App.Payment.DAL
{
    public class Incharge
    {
        public static string currTableName = WebConfig.TablePrefix + "p_incharge";





        public bool Add(InchargeInfo model)
        {
            return DbHelper.ExecuteSql(GetAddSql(model))>0 ;
        }

        internal static string GetAddSql(InchargeInfo model)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into " + currTableName + " (");
            strSql.Append("sys_model_id,[no],admin_id,user_id,[state],[money],payonline_id,create_time,[type])");
            strSql.Append(" values (");
            strSql.Append(" " + model.sys_model_id + ",");
            strSql.Append("'" + StringHelper.SqlFilter(model.no) + "',");
            strSql.Append(" " + model.admin_id.ToString() + ",");
            strSql.Append(" " + model.user_id.ToString() + ",");
            strSql.Append(" " + model.state.ToString() + ",");
            strSql.Append(" " + model.money.ToString() + ",");
            strSql.Append(" " + model.payonline_id.ToString() + ",");
            strSql.Append("'" + model.create_time.ToString() + "',");

            strSql.Append(" " + model.type.ToString());
            strSql.Append(")");

            return strSql.ToString();
        }



        public bool Delete(int id, string andWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from [" + currTableName + "] ");
            strSql.Append(" where id=" + id.ToString() + " " + andWhere);

            return DbHelper.ExecuteSql(strSql.ToString()) > 0;
        }








        private static IDataReader GetReader(int topCount, string colNames, string andWhere)
        {
            string sql = "select " + (topCount == -1 ? "" : "top " + topCount) + " " + colNames + " from " + currTableName + " where 1=1 " + andWhere;
            return DbHelper.ExecuteReader(sql);
        }



        public DataTable GetList(int sysModelId,string andWhere)
        {
            return DbHelper.GetDataTable(string.Format("select * from [" + currTableName + "] where sys_model_id={0} {1}",sysModelId, andWhere));
        }


        public DataTable GetList(int sysModelId, int topCount, string andWhere)
        {
            string tmpTopCount = (topCount <= 0) ? "" : " top " + topCount.ToString();
            return DbHelper.GetDataTable(string.Format("select {0} * from [" + currTableName + "] where sys_model_id={1} {2}", tmpTopCount, sysModelId, andWhere));

        }

        public DataTable GetList(int sysModelId, int topCount, int currPageCount, string andWhere)
        {
            string sql = Sql.GetRePagingString(topCount, currPageCount, currTableName, "id", " and sys_model_id=" + sysModelId +andWhere);
            return DbHelper.GetDataTable(sql);
        }

        public DataTable GetList(int sysModelId, int topCount, int currPageCount, string colNames, string andWhere)
        {
            string sql = Sql.GetRePagingString(topCount, currPageCount, currTableName, "id", colNames, " and sys_model_id=" + sysModelId + andWhere);
            return DbHelper.GetDataTable(sql);
        }

        public DataTable GetList(int sysModelId, int topCount, int currPageCount, string andWhere, LeftJoinInfo joininfo)
        {

            string sql = Sql.GetRePagingString(topCount, currPageCount, currTableName, "id", "*", " and sys_model_id=" + sysModelId + andWhere, joininfo);
            return DbHelper.GetDataTable(sql);
        }



        public int GetCount(int sysModelId, string andWhere)
        {
            string sql = "select count(1) from [" + currTableName + "] where sys_model_id=" + sysModelId + andWhere;
            object obj = DbHelper.GetSingle(sql);
            return obj == null ? 0 : int.Parse(obj.ToString());
        }


        private static InchargeInfo GetInchargeInfo(IDataReader dr)
        {
            InchargeInfo model = null;
            if (dr != null)
            {
                if (dr.Read())
                {
                    model = new InchargeInfo();
                    model.id = int.Parse(dr["id"].ToString());
                    model.sys_model_id = int.Parse(dr["sys_model_id"].ToString());
                    model.no = dr["no"].ToString();
                    model.admin_id = int.Parse(dr["admin_id"].ToString());
                    model.user_id = int.Parse(dr["user_id"].ToString());
                    model.state = int.Parse(dr["state"].ToString());
                    model.money = double.Parse(dr["money"].ToString());
                    model.payonline_id = int.Parse(dr["payonline_id"].ToString());
                    model.create_time = DateTime.Parse(dr["create_time"].ToString());
                    model.type = int.Parse(dr["type"].ToString());
                }
                dr.Close();
            }
            return model;
        }


        public static InchargeInfo GetInchargeInfo(int id)
        {
            IDataReader dr = GetReader(1, "*", " and id=" + id.ToString());
            return GetInchargeInfo(dr);
        }


        public static InchargeInfo GetInchargeInfo(string no)
        {
            IDataReader dr = GetReader(1, "*", " and no='" +StringHelper.SqlFilter(no) +  "'");
            return GetInchargeInfo(dr);
        }


    }
}

